package one.stand.controller;

import com.jmp.api.crm.dto.CardApplyAgreeRequest;
import com.jmp.api.crm.dto.CardApplyRefuseRequest;
import com.jmp.api.crm.dto.CardApplyResponse;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.base.dto.*;
import com.jmp.feign.CardFeign;
import com.jmp.feign.CompanyFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import one.stand.dto.ConvertUtils;
import one.stand.dto.PageRequest;
import one.stand.dto.PageResponse2;
import one.stand.model.ResultModel;
import one.stand.request.BusinessCardRequest;
import one.stand.response.GetCompanyStaffResponse;
import one.stand.util.AssertUtil;
import one.stand.util.RequestContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("crmPc/businessCard")
@Api(description = "成员管理")
public class BusinessCardController extends BaseController {

    @Autowired
    private CardFeign cardFeign;

    @Autowired
    private CompanyFeign companyFeign;

    @ApiOperation(value = "得到公司员工信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cardId", value = "员工id", required = true, paramType = "Integer"),
    })
    @PostMapping("getCompanyStaffInfo")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel<GetCompanyStaffResponse> getCompanyStaffInfo(@RequestAttribute BusinessCardRequest request) {

        return crmFegin.getCompanyStaffInfo(request);
    }

    @ApiOperation(value = "企业员工退出企业")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cardId", value = "退出员工名片id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "userId", value = "退出员工userId", required = true, paramType = "Integer"),
    })
    @PostMapping("quitCompany")
    @SecurityAuthCheck(company = true, card = true, isBoss = true)
    public ResultModel quitCompany(@RequestAttribute BusinessCardRequest request) {
        final Integer userCardId = RequestContextHolder.getRequestContext().getUser().getCardId();

        return companyFeign.quit(CompanyQuitRequest.builder()
                .cardId(request.getCardId())
                .myCardId(userCardId)
                .build());
    }

    @ApiOperation("2员工申请列表")
    @GetMapping("apply")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel<CardApplyResponse> apply(@RequestAttribute PageRequest request) {
        final Integer optCompanyId = RequestContextHolder.getRequestContext().getUser().getCompanyId();
        CardListDto cardListDto = CardListDto.builder()
                .companyId(optCompanyId)
                .status(0)
                .build();
        cardListDto.setPageNum(request.getPageNum());
        cardListDto.setPageSize(request.getPageSize());
        ResultModel<CardListVo> cardListVoResultModel = cardFeign.list(cardListDto);
        AssertUtil.isTrue(cardListVoResultModel);
        PageResponse2<CardApplyResponse.Card> cardPage = ConvertUtils.page2(cardListVoResultModel.getData().getCardPage(), card -> {
            return CardApplyResponse.Card.builder()
                    .cardId(card.getCardId())
                    .logo(card.getLogo())
                    .name(card.getName())
                    .time(card.getTime().getTime())
                    .build();
        });
        CardApplyResponse response = CardApplyResponse.builder()
                .cardPage(cardPage)
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation("2员工申请:同意")
    @PostMapping("apply/agree")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel applyAgree(@RequestAttribute CardApplyAgreeRequest request) {

        ResultModel resultModel = cardFeign.applyAgree(CardApplyAgreeDto.builder().cardId(request.getCardId()).build());
        AssertUtil.isTrue(resultModel);

        return ResultModel.success();
    }

    @ApiOperation("2员工申请:拒绝")
    @PostMapping("apply/refuse")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel applyRefuse(@RequestAttribute CardApplyRefuseRequest request) {

        ResultModel resultModel = cardFeign.applyRefuse(CardApplyRefuseDto.builder()
                .cardId(request.getCardId())
                .build());
        AssertUtil.isTrue(resultModel);

        return ResultModel.success();
    }
}
